﻿Namespace DGZfP.EventPublisher
    Partial Class Mapping
        Inherits System.Web.UI.UserControl

        Private ColumnsValue As DataColumnCollection

        Public Property Columns() As DataColumnCollection
            Get
                Return ColumnsValue
            End Get
            Set(ByVal value As DataColumnCollection)
                ColumnsValue = value
                If value Is Nothing Then ddlColumn.Items.Clear()
            End Set
        End Property

        Public Property Text() As String
            Get
                If ddlColumn.Visible Then
                    Return ddlColumn.SelectedValue
                Else
                    Return CStrN(ViewState("Mapping"))
                End If
            End Get
            Set(ByVal value As String)

                ViewState("Mapping") = value

            End Set
        End Property

        Public Property Type() As String
            Get
                Return CStrN(ViewState("Type"))
            End Get
            Set(ByVal value As String)
                ViewState("Type") = value
            End Set
        End Property

        Public Property AlternateValues() As String
            Get
                Return CStrN(ViewState("AlternateValues"))
            End Get
            Set(ByVal value As String)
                ViewState("AlternateValues") = value
            End Set
        End Property

        Private ReadOnly Property SystemType() As Type
            Get
                Select Case Type.ToLowerInvariant
                    Case "string"
                        Return GetType(String)
                    Case "integer"
                        Return GetType(Integer)
                    Case "datetime"
                        Return GetType(DateTime)
                    Case "boolean"
                        Return GetType(Boolean)
                End Select

            End Get
        End Property



        Public Property EditMode() As Boolean
            Get
                Return ddlColumn.Visible
            End Get
            Set(ByVal value As Boolean)
                SwitchEditMode(value)
            End Set
        End Property

        Private MappingsValue As Generic.IDictionary(Of String, String)

        Public Property Mappings() As Generic.IDictionary(Of String, String)
            Get
                If MappingsValue Is Nothing Then
                    MappingsValue = New Generic.Dictionary(Of String, String)
                    MappingsValue.Add("", ">>Not Set<<")
                    If Not Columns Is Nothing Then
                        For Each col As DataColumn In Columns
                            If Type.Length > 0 Then
                                If col.DataType Is SystemType Then _
                                    MappingsValue.Add(col.ColumnName, col.ColumnName)
                            Else
                                If col.DataType Is GetType(String) OrElse col.DataType Is GetType(Integer) Then _
                                    MappingsValue.Add(col.ColumnName, col.ColumnName)
                            End If

                        Next
                    End If
                    For Each alternate As String In AlternateValues.Split(";"c)
                        If alternate <> String.Empty Then MappingsValue.Add(String.Format("""{0}""", alternate), String.Format("""{0}"" (Constant)", alternate))
                    Next
                    If Not MappingsValue.ContainsKey(Text) Then
                        If EditMode Then
                            MappingsValue.Add(Text, String.Format("Not found: [{0}]", Text))
                        Else
                            MappingsValue.Add(Text, String.Format("[{0}]", Text))
                        End If

                    End If

                End If
                Return MappingsValue
            End Get
            Set(ByVal value As Generic.IDictionary(Of String, String))
                MappingsValue = value
            End Set
        End Property

        Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
            If Not IsPostBack And Text <> String.Empty Then Me.DataBind()
            MyBase.OnLoad(e)
        End Sub

        Private Sub SwitchEditMode(ByVal IsEditMode As Boolean)
            ddlColumn.Items.Clear()
            Me.DataBind()
            lblColumn.Visible = Not IsEditMode
            ddlColumn.Visible = IsEditMode

        End Sub


    End Class
End Namespace
